package main

import "fmt"

type Book struct {
	Bookname string
	price    float64
}
type person struct {
	name string
	age  int
	book Book //嵌套结构体就说明一个人只能有一本书
}
type person4 struct {
	name string
	age int
	books []Book //嵌套一个切片就可实现一个人有很多本书，可以用range遍历
}

func main() {
	b1 := Book{}
	b1.Bookname = "金平里没有梅"
	b1.price = 35.8
	fmt.Println(b1)
	p1 := person{}
	p1.name = "王二狗" //string
	p1.age = 30     //int
	p1.book = b1    //Book
	fmt.Println(p1)

	b2 := Book{"十万个为什么", 44.8}
	p2 := person{"李小花", 18, b2}
	fmt.Println(p2)

	p3 := person{
		name: "rose",
		age:  30,
		book: Book{
			Bookname: "西游记",
			price:    98.9,
		},
	}
	fmt.Println(p3)

	p4 := person{}
	p4.name = "jack"
	p4.age = 28
	p4.book = Book{
		Bookname: "红楼梦",
		price:    5.6,
	}
	fmt.Println(p4)
	fmt.Println("------------")

	p5:=person4{}
	p5.name="花花"
	p5.age=18
	b3:=Book{"王二狗流浪记",88.6}
	b4:=Book{"呼呼",28.6}
	books1:=[]Book{b3,b4,Book{"茶花女",66.4}}
	fmt.Println(books1)
	p5.books=books1
	fmt.Println(p5)

	for i,v:=range p5.books{
		fmt.Println(i+1,v.Bookname,v.price)
	}
	p6:=person4{"如花",18,make([]Book,0,10)}
	p6.books=append(p6.books,Book{"火影",34.4})
	p6.books=append(p6.books,Book{"熊出没",42.4})
	p6.books=append(p6.books,Book{"天线宝宝",12.4})
	p6.books=append(p6.books,Book{"小猪配起",78.4})

	fmt.Println(p6)



}
